package com.nanhai.buddhist.college.module.collegebase.service.textbookinfo.info;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info.vo.TextbookInfoCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info.vo.TextbookInfoExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info.vo.TextbookInfoPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info.vo.TextbookInfoUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.textbookinfo.info.TextbookInfoConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.textbookinfo.TextbookInfoDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.textbooktype.TextbookTypeDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.textbookinfo.TextbookInfoMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.textbookinfo.TextbookTypeMapper;
import com.nanhai.buddhist.college.module.collegebase.service.textbookinfo.type.TextbookTypeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.TEXTBOOK_INFO_NOT_EXISTS;

/**
 * 教材列表 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class TextbookInfoServiceImpl implements TextbookInfoService {

    @Resource
    private TextbookInfoMapper textbookInfoMapper;

    @Resource
    private TextbookTypeMapper textbookTypeMapper;


    @Resource
    private TextbookTypeService typeService;

    @Override
    public String createTextbookInfo(TextbookInfoCreateReqVO createReqVO) {
        // 插入
        TextbookInfoDO textbookInfo = TextbookInfoConvert.INSTANCE.convert(createReqVO);
        textbookInfoMapper.insert(textbookInfo);
        // 返回
        return textbookInfo.getId();
    }

    @Override
    public void updateTextbookInfo(TextbookInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTextbookInfoExists(updateReqVO.getId());
        // 更新
        TextbookInfoDO updateObj = TextbookInfoConvert.INSTANCE.convert(updateReqVO);
        textbookInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteTextbookInfo(String id) {
        // 校验存在
        this.validateTextbookInfoExists(id);
        // 删除
        textbookInfoMapper.deleteById(id);
    }

    private void validateTextbookInfoExists(String id) {
        if (textbookInfoMapper.selectById(id) == null) {
            throw exception(TEXTBOOK_INFO_NOT_EXISTS);
        }
    }

    @Override
    public TextbookInfoDO getTextbookInfo(String id) {
        TextbookInfoDO textbookInfoDO = textbookInfoMapper.selectById(id);

        // 查询分类  父级集合
        List<String> parentIds = new ArrayList<>();
        TextbookTypeDO textbookTypeDO = textbookTypeMapper.selectById(textbookInfoDO.getTypeId());
        getParentIds(textbookTypeDO, parentIds);

        textbookInfoDO.setParentIds(parentIds);
        return textbookInfoDO;
    }

    private List<String> getParentIds(TextbookTypeDO textbookTypeDO, List<String> parentIds) {

        if (ObjectUtil.isNotNull(textbookTypeDO) && !"0".equals(textbookTypeDO.getParentId())) {
            parentIds.add(textbookTypeDO.getParentId());
            TextbookTypeDO textbookTypeDO1 = textbookTypeMapper.selectById(textbookTypeDO.getParentId());
            getParentIds(textbookTypeDO1, parentIds);
        }

        return parentIds;
    }


    @Override
    public List<TextbookInfoDO> getTextbookInfoList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : textbookInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TextbookInfoDO> getTextbookInfoPage(TextbookInfoPageReqVO pageReqVO) {

        PageResult<TextbookInfoDO> textbookInfoDOPageResult = textbookInfoMapper.selectPage(pageReqVO);

        return textbookInfoDOPageResult;
    }

    @Override
    public List<TextbookInfoDO> getTextbookInfoList(TextbookInfoExportReqVO exportReqVO) {
        return textbookInfoMapper.selectList(exportReqVO);
    }

    @Override
    public List<TextbookInfoDO> getTextbookInfoAllList() {
        LambdaQueryWrapper<TextbookInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        return textbookInfoMapper.selectList(queryWrapper);
    }

}
